package com.dbecom.platform.foundation.batch.config;

import org.apache.tomcat.jdbc.pool.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import com.mysql.jdbc.Driver;

@Configuration
@PropertySource("classpath:/batch-datasource.properties")
public class DataSourceConfig {

    @Bean(name = "batchDataSource")
    public DataSource batchDataSource(Environment environment) throws Exception {

        String user = environment.getProperty("batch.jdbc.username"), pw = environment
                .getProperty("batch.jdbc.password"), url = environment.getProperty("batch.jdbc.url"), initialSize = environment
                .getProperty("batch.jdbc.pool.initialSize"), maxActive = environment
                .getProperty("batch.jdbc.pool.maxActive");

        Class<Driver> driverClass = environment.getPropertyAsClass("batch.jdbc.driverClass", Driver.class);

        DataSource dataSource = new DataSource();
        dataSource.setDriverClassName(driverClass.getName());
        dataSource.setPassword(pw);
        dataSource.setUrl(url);
        dataSource.setUsername(user);

        dataSource.setInitialSize(Integer.parseInt(initialSize));
        dataSource.setMaxActive(Integer.parseInt(maxActive));

        return dataSource;
    }
}